import java.util.LinkedList;
import java.util.List;


public class DSF {

	static class Vertex {
		boolean visited = false;
		String name;
		List<Vertex> adjacent = new LinkedList<Vertex>();
		
		public Vertex(String name) {
			this.name = name;
		}
		public void visit(){
			System.out.println(name);
		}
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {

		Vertex a = new Vertex("A");
		Vertex b = new Vertex("B");
		Vertex b1 = new Vertex("B1");
		Vertex b2 = new Vertex("B2");
		Vertex c = new Vertex("C");
		Vertex c1 = new Vertex("C1");
		Vertex c2 = new Vertex("C2");
		Vertex d = new Vertex("D");
		
		a.adjacent.add(b);
		
		b.adjacent.add(a);
		b.adjacent.add(b1);
		b1.adjacent.add(b2);
		b.adjacent.add(c);
		
		c.adjacent.add(b);
		c.adjacent.add(c1);
		c1.adjacent.add(c2);
		c.adjacent.add(d);
		
		d.adjacent.add(c);
		
		dsf(a);
//		bsf(a);
	}


	private static void bsf(Vertex v) {
		v.visit();
		v.visited = true;
		LinkedList<Vertex> queue = new LinkedList<Vertex>();
		queue.add(v);
		while (!queue.isEmpty()) {
			Vertex u = queue.removeFirst();
			for (Vertex adj : u.adjacent) {
				if (!adj.visited){
					adj.visit();
					adj.visited = true;
					queue.add(adj);
				}
			}
		}
	}


	private static void dsf(Vertex v) {
		v.visit();
		v.visited = true;
		for (Vertex u : v.adjacent) {
			if (!u.visited)
				dsf(u);
		}
	}

	
}
